package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.CookieRedis;
import com.ly.mpayment.core.util.FileUtils;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.entity.SysConfig;
import com.ly.mpayment.persist.service.sys.ISysConfigService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;
import com.ly.mpayment.rest.init.SystemInit;

import net.sf.json.JSONObject;

@Component
@Path("system/sysconfig/")
public class SysConfigRest extends AbsRest {

	@Autowired
	ISysConfigService configService;
	@Autowired
	IUserService userService;
	@Autowired
	SystemInit init;

	@SuppressWarnings("static-access")
	@Path("findPageConfig")
	@POST
	public String findPageConfig(String data,@Context HttpServletRequest request){
		String result = this.validationParameterGetRole(request,userService);
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("attr"))){
				parameters.put(":attr", json.getString("attr"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(configService.findPageAll(parameters, json.getInt("start"), json.getInt("pageSize")),
					new String[]{""}));
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("saveConfig")
	@POST
	public String saveConfig(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,  "attr","describes","attrValue");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			SysConfig config = new SysConfig();
			config.setAttrValue(json.getString("attrValue"));
			config.setDescribes(json.getString("describes"));
			config.setAttr(json.getString("attr"));
			if(configService.saveSysConfig(config)){
				CookieRedis.addCookie(config.getAttr(), config.getAttrValue());
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("updateConfig")
	@POST
	public String updateConfig(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService, "configId","attr","describes","attrValue");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			SysConfig config = configService.findById(json.getString("configId"));
			if(config != null){
				config.setAttrValue(json.getString("attrValue"));
				config.setDescribes(json.getString("describes"));
				config.setAttr(json.getString("attr"));
				if(configService.updateSysConfig(config)){
					CookieRedis.addCookie(config.getAttr(), config.getAttrValue());
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@Path("deleteConfig")
	@POST
	public String deleteConfig(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService, "configId");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			SysConfig config = configService.findById(json.getString("configId"));
			if(config != null){
				if(configService.deleteConfig(config)){
					CookieRedis.rmKey(config.getAttr());
					return ResultMessage.getSucceed(null);
				}
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}
}
